package com.household.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.household.domain.ResponseResult;
import com.household.domain.entity.User;
import com.household.domain.enums.ResponseErrorCodeEnum;
import com.household.domain.enums.RoleEnum;
import com.household.domain.vo.AdminUserVO;
import com.household.domain.vo.BootstrapTableVO;
import com.household.feigns.LoginClient;
import com.household.service.UserService;
import com.household.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * Created By wanzhen
 * 2023. 2023/4/7. 23:22
 */

@RestController
@RequestMapping("/admin")
public class UserController {
    private final LoginClient loginClient;
    private final UserService userService;

    @Autowired
    public UserController(LoginClient loginClient, UserService userService) {
        this.loginClient = loginClient;
        this.userService = userService;
    }

    @RequestMapping("/user/list")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public BootstrapTableVO<AdminUserVO> listUser(@RequestParam(required = false, defaultValue = "1") Integer current,
                                                  @RequestParam(required = false, defaultValue = "10") Integer size,
                                                  String isReal, String isEnable, String isLock, String role,
                                                  String id, String username, String tel, @AuthenticationPrincipal User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Integer enable = StringUtils.toInteger(isEnable, -1);
        if (enable != -1) {
            wrapper.eq("has_enable", enable);
        }
        Integer lock = StringUtils.toInteger(isLock, -1);
        if (lock == 1) {
            wrapper.ge("lock_date", LocalDateTime.now());
        } else if (lock == 0) {
            wrapper.isNull("lock_date").or().le("lock_date", LocalDateTime.now());
        }
        Integer real = StringUtils.toInteger(isReal, -1);
        if (real == 1) {
            wrapper.isNotNull("real_name").isNotNull("id_card");
        } else if (real == 0) {
            wrapper.isNull("real_name").or().isNull("id_card");
        }
        Integer roleType = StringUtils.toInteger(role, -2);
        RoleEnum roleEnum = RoleEnum.getByType(roleType);
        if (roleEnum != null) {
            wrapper.eq("role_id", roleType);
        }

        if (StringUtils.isNotBlank(id)) {
            wrapper.eq("id", id);
        } else {
            // 不显示当前用户
            wrapper.ne("id", user.getId());
        }
        if (StringUtils.isNotBlank(username)) {
            wrapper.like("username", username);
        }
        if (StringUtils.isNotBlank(tel)) {
            wrapper.eq("tel", tel);
        }


//        userService.pageAdminUserInfoVO(new Page<>(current,size),wrapper);
        return userService.pageAdminUserInfoVO(new Page<>(current,size),wrapper);

    }

    @PostMapping("/user/{id}/status")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResponseResult changeStatus(@PathVariable String id, String type, String hour){
        Integer op = StringUtils.toInteger(type, -1);
        User user = userService.getById(id);
        if(user.getRole() == RoleEnum.ADMIN) {
            return ResponseResult.failure(ResponseErrorCodeEnum.NO_PERMISSION);
        }

        switch (op){
            case 1:
                user.setHasEnable(0);
                break;
            case 2:
                user.setHasEnable(1);
                break;
            case 3:
                // 必须为正整数
                Integer lockHour = StringUtils.toInteger(hour, -1);
                if(lockHour == -1 || lockHour < 0) {
                    return ResponseResult.failure(ResponseErrorCodeEnum.MUST_POSITIVE_INTEGER);
                }
                user.setLockDate(LocalDateTime.now().plusHours(lockHour));
                break;
            case 4:
                user.setLockDate(LocalDateTime.now());
                break;
            default:
                return ResponseResult.failure(ResponseErrorCodeEnum.PARAMETER_ERROR);
        }
        if (userService.updateById(user)){
            return ResponseResult.success();
        }else {
            return ResponseResult.failure(ResponseErrorCodeEnum.OPERATION_ERROR);
        }

    }
}
